﻿// Copyright © Microsoft Corporation.  All Rights Reserved.
// This code released under the terms of the 
// Microsoft Public License (MS-PL, http://opensource.org/licenses/ms-pl.html.)

using System;
using Microsoft.Phone.Controls;
using System.Windows;

namespace TheOliver.Phone.Tools.Controls
{
    public partial class NumberPickerPage : PhoneApplicationPage
    {
        public NumberPickerPage()
        {
            InitializeComponent();

            digitLoopingSelector1.IsExpandedChanged += HandleSelectorIsExpandedChanged;
            digitLoopingSelector2.IsExpandedChanged += HandleSelectorIsExpandedChanged;
            digitLoopingSelector3.IsExpandedChanged += HandleSelectorIsExpandedChanged;
            digitLoopingSelector4.IsExpandedChanged += HandleSelectorIsExpandedChanged;
        }

        private void HandleSelectorIsExpandedChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            if ((bool)e.NewValue)
            {
                // Ensure that only one selector is expanded at a time
                digitLoopingSelector1.IsExpanded = (sender == digitLoopingSelector1);
                digitLoopingSelector2.IsExpanded = (sender == digitLoopingSelector2);
                digitLoopingSelector3.IsExpanded = (sender == digitLoopingSelector3);
                digitLoopingSelector4.IsExpanded = (sender == digitLoopingSelector4);
            }
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            string s = NavigationContext.QueryString["v"];
            double v = Convert.ToDouble(s);
            NumberValue = v;

            bool showDecimal = bool.Parse(NavigationContext.QueryString["d"]);
            if (!showDecimal)
            {
                ContentPanel.ColumnDefinitions[3].Width = new GridLength(0);
                ContentPanel.ColumnDefinitions[4].Width = new GridLength(0);
            }

            //string title = NavigationContext.QueryString["t"];
            //this.Title = title;

            base.OnNavigatedTo(e);
        }

        private void SaveValue()
        {
            int v1 = (int)digitLoopingSelector1.SelectedItem;
            int v2 = (int)digitLoopingSelector2.SelectedItem;
            int v3 = (int)digitLoopingSelector3.SelectedItem;
            int v4 = (int)digitLoopingSelector4.SelectedItem;

            double result = v1 * 100 + v2 * 10 + v3 + ((double)v4) / 10.0;
            NumberValue = Convert.ToDouble(result);
        }

        private double _numberValue;
        public double NumberValue
        {
            get { return _numberValue; }
            set 
            { 
                _numberValue = value;

                string h = _numberValue.ToString("000.0");
                int v1 = int.Parse(h[0].ToString());
                int v2 = int.Parse(h[1].ToString());
                int v3 = int.Parse(h[2].ToString());
                int v4 = int.Parse(h[4].ToString());
                
                digitLoopingSelector1.SelectedItem = v1;
                digitLoopingSelector2.SelectedItem = v2;
                digitLoopingSelector3.SelectedItem = v3;
                digitLoopingSelector4.SelectedItem = v4;

                digitLoopingSelector1.DefaultValue = v1;
                digitLoopingSelector2.DefaultValue = v2;
                digitLoopingSelector3.DefaultValue = v3;
                digitLoopingSelector4.DefaultValue = v4;
            }
        }

        //private string _numberTitle = "Number Value";
        //public string NumberTitle
        //{
        //    get { return _numberTitle; }
        //    set { _numberTitle = value; _title.Text = _numberTitle;  }
        //}

        private void _okButton_Click(object sender, System.EventArgs e)
        {
        	SaveValue();
                    NavigationService.GoBack();
        }

        private void _cancelButton_Click(object sender, System.EventArgs e)
        {
        	 NavigationService.GoBack();
        }

    }
}